﻿using System;
using System.Data;
using Core.Message.Entities;
using Core.ExceptionHandling;
using Core.Controls.Message;

using Core.Common.Session;
using vHCS.HR.Business;
using vHCS.HR.Entities;

namespace vHCS.K2App.MdlK2.Controls
{
    public partial class HR_ctrEmpHeader_Info : Core.NTier.UserInterface.BaseUserControl
    {
        #region Public Properties
        /// <summary>
        /// Username của nhân viên cần hiển thị thông tin
        /// </summary>
        public string Username
        {
            set { this.hdfUsername.Value = value; }
            get { return this.hdfUsername.Value.Trim(); }
        }

        /// <summary>
        /// Mã nhân viên
        /// </summary>
        public long EmployeeId
        {
            get
            {
                long lEmpId = 0;
                long.TryParse(hdfEmployeeId.Value, out lEmpId);
                return lEmpId;
            }
        }
        /// <summary>
        ///Tự động load thông tin nhân viên theo tên đăng nhập hiện tại
        /// </summary>
        public bool AutoLoad
        {
            set { this.hdfAutoLoad.Value = value.ToString(); }
            get
            {
                bool autoload = false;
                bool.TryParse(this.hdfAutoLoad.Value.Trim(), out autoload);
                return autoload;
            }
        }

        public string EmpCode
        {
            get { return txtEmpCode.Text.Trim(); }
        }

        public string FullName
        {
            get { return txtFullName.Text.Trim(); }
        }

        public string JobTitle
        {
            get { return txtJobTitle.Text.Trim(); }
        }

        public string JobLevel
        {
            get { return txtRankingLevel.Text.Trim(); }
        }

        public string ReportingLine
        {
            get { return txtReportFor.Text.Trim(); }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadDefaultUser();
            }

        }

        /// <summary>
        /// Hiển thị thông tin user mặc định
        /// </summary>
        private void LoadDefaultUser()
        {
            try
            {
                if (this.AutoLoad)
                {
                    string username = Username;
                    if (string.IsNullOrEmpty(username)) username = SessionManager.UserDomain;
                    this.LoadEmpInfo(username);
                }
            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }

        /// <summary>
        /// Hiển thị thông tin nhân viên
        /// </summary>
        public void LoadEmpInfo(string username)
        {
            try
            {
                HR_EmpCVCollection controller = new HR_EmpCVCollection();
                long empId = controller.GetEmpIdByUsername(username);
                this.LoadEmpInfoByEmpId(empId);

            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }

        public void LoadEmpInfoByEmpId(long empId)
        {
            try
            {
                this.ClearForm();
                HR_EmpCVCollection controller = new HR_EmpCVCollection();
                HR_EmpHeaderDTO empInfo = controller.GetEmpHeader(empId);
                if (empInfo != null)
                {
                    this.txtEmpCode.Text = empInfo.EmpCode;
                    this.txtFullName.Text = empInfo.FullName;
                    this.txtBarcode1.Text = empInfo.Barcode;
                    this.txtOrgchart.Text = empInfo.OrgChart;
                    this.txtMgtJob.Text = string.IsNullOrEmpty(empInfo.MgtJob) ? "" : empInfo.MgtJob;
                    this.txtProJob.Text = string.IsNullOrEmpty(empInfo.ProJob) ? "" : empInfo.ProJob;
                    this.txtEmptype.Text = empInfo.EmpType;
                    this.txtContractType.Text = empInfo.ContactType;
                    this.txtWorkingPlace.Text = empInfo.WorkingOffice;
                    this.txtJobTitle.Text = empInfo.JobTitle;
                    this.txtRankingLevel.Text = empInfo.JobLevel;
                    this.txtReportFor.Text = empInfo.ReportTo;
                    if (empInfo.JoinDate.Year > 1)
                        this.txtJoinDate.Text = empInfo.JoinDate.ToShortDateString();

                    this.rbtTerminationStatus.SelectedValue = empInfo.Active.ToString();
                    this.hdfEmployeeId.Value = empInfo.EmployeeId.ToString();
                    this.txtUserName.Text = empInfo.UserName;
                }
            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }

        /// <summary>
        /// Xóa thông tin của form
        /// </summary>
        protected void ClearForm()
        {
            this.txtEmpCode.Text = null;
            this.txtFullName.Text = null;
            this.txtBarcode1.Text = null;
            this.txtOrgchart.Text = null;
            this.txtMgtJob.Text = null;
            this.txtProJob.Text = null;
            this.txtEmptype.Text = null;
            this.txtContractType.Text = null;
            this.txtWorkingPlace.Text = null;
            this.txtJobTitle.Text = null;
            this.txtRankingLevel.Text = null;
            this.txtReportFor.Text = null;
            this.txtJoinDate.Text = null;
            this.rbtTerminationStatus.SelectedValue = null;
            this.hdfEmployeeId.Value = null;
            this.txtUserName.Text = null;
        }
    }
}